class ScheduleController < ApplicationController
  
  require 'array_overrides'

  before_filter :assign_user #to be removed

  def index   
    @student = Student.find(session['student_id']) 
    #checks to make sure the student has future semesters, and if not then populate his/her futures
    if (@student.futures.nil? or @student.futures == []) and (@student.courses_taken.length < @student.major.required_courses.length) #Last condition not perfect
      fill_future(@student)
    end
    @semesters = Semester.current_and_future
    @current_semester_id = @student.current_open_registration.semester.id if @student.current_open_registration
    @futures = @student.futures
    
    #for search
    @results = []
    @all_semesters_for_searched_courses = @semesters.collect { |e| "semester_" + e.element_id}.join(' ').sub(/^semester_/, '')
  end

  def move_course
    
    if (params[:id] == "search_MATH4320")# and (params[:semester_id] == 7)
      @error = true
    else
      @error = false
    end
   @new_semester = Semester.find(params[:semester_id])
   #logger.warn("_______\n\n\n"+ params[:semester_id] + "\n__________\n\n\n")
   @course = Course.with_number(params[:id].gsub(/^search_(.*)/, '\1'))
   @student = Student.find(params[:student_id])
   @new_semester_hours = @student.hours_in_future_semester(@new_semester) + @course.hours
   
   future = @student.future_with_course(@course)
   unless future.nil?
     old_semester = future.semester
     @old_semester_element_id = old_semester.element_id.clone
     @old_semester_hours = @student.hours_in_future_semester(old_semester) - @course.hours
     future.semester = @new_semester
   else
     future = Future.new
     future.semester = @new_semester
     future.course = @course
     future.student = @student
   end
   future.save
  end
  
  def live_search
    student = Student.find(session['student_id'])
    #title = request.raw_post || request.query_string
    #@title = title.clone.gsub('&', "<br>")
    requirements = []

    requirements << "title like \"%#{params[:search_course_title]}%\"" if params[:search_course_title] != "all"
    requirements << "department_id = #{params[:search_department]}" if params[:search_department] != "all"
    if requirements == []
      @results = Course.find(:all)
    else
      @results = Course.find(:all, :conditions => requirements.join(' and '))
    end
    @results = @results.intersection(student.major.technical_electives) if params[:electives] == 'technical'
    
    @all_semesters_for_searched_courses = Semester.current_and_future.collect { |e| 'semester_' + e.element_id }.join(' ').gsub(/^semester_(.*)/, '\1')
    
    
    @student = student
    render(:layout => false)
  end
  
  def remove_course
    course = Course.find(params[:course_id])
    student = Student.find(params[:student_id])
    
    future_with_course = student.future_with_course(course)
    @semester = future_with_course.semester
    @new_hours = student.hours_in_future_semester(@semester) - course.hours
    future_with_course.destroy unless future_with_course.nil?
    

    @course_element_id = course.number
  end
  
  def choose_section
    chosen = Section.find(params[:chosen_section_id])
    current = Section.find(params[:current_section_id])
    course = Course.find(params[:course_id])
    
    chosen.registered = true
    chosen.save
    current.registered = true
    current.save
    
    render :partial => "current_course", :locals => { :course => course, :course_element_id => course.number, :semester_element_id => course.registered_section.semester.element_id, :student => Student.find(session['student_id']), :in_search_results => false, :section => course.registered_section }
  end

  def create
  end

  def edit
  end

  def update
  end

  def destroy
  end
  
  private
  #to be removed
  def assign_user
    session['student_id'] = 1
    session['max_hours'] = 15
  end
  
  def fill_future(student)
    if student.major.id == 1
      #Fall 2007 (id = 7)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("ENGL1101").id, :semester_id => 7)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("MATH1501").id, :semester_id => 7)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("HPS1040").id, :semester_id => 7)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("CS1321").id, :semester_id => 7)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("HIST2112").id, :semester_id => 7)
      
      #Spring 2008 (id = 8)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("ENGL1102").id, :semester_id => 8)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("CHEM1301").id, :semester_id => 8)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("MATH1502").id, :semester_id => 8)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("CS1322").id, :semester_id => 8)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("CS1050").id, :semester_id => 8)
      
      #Summer 2008 (id = 9)
      
      #Fall 2008 (id = 10)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("MATH2401").id, :semester_id => 10)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("PHYS2211").id, :semester_id => 10)
      #put 3 hours of Humanities electives here
      #put 6 hours of SS electives here
      
      #Spring 2009 (id = 11)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("MATH2406").id, :semester_id => 11)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("MATH2602").id, :semester_id => 11)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("PHYS2212").id, :semester_id => 11)
      # => put 3 hours of Humanities electives here
      # => put 3 hours of SS electives here
      
      #Summer 2009 (id =12)
      
      #Fall 2009 (id = 13)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("MATH3012").id, :semester_id => 13)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("CS3510").id, :semester_id => 13)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("CS2110").id, :semester_id => 13)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("MATH4150").id, :semester_id => 13)
      # => put 3 hours of tech electives here
      
      #Spring 2010 (id = 14)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("MATH3215").id, :semester_id => 14)
      student.futures << Future.create(:student_id => student.id, :course_id => Course.with_number("CS4510").id, :semester_id => 14)
    end
  end
  
  # def fill_future(student)
  #   max_hours_about = 15
  #   major = student.major
  #   semesters = Semester.current_and_future
  #   taken = student.courses_taken
  #   
  #   hours = 0
  #   i = 0
  #   major.required_courses.sort.each do |req_course|
  #     if taken.include? req_course
  #       next
  #     end
  #     future = Future.new
  #     future.course = req_course
  #     
  #     if hours > 15
  #       i = i+1
  #       hours = 0
  #     end
  #     future.semester = semesters[i]
  #     hours = hours + future.course.hours
  #     student.futures << future
  #     future.save
  #     student.save
  #   end
  # end
end
